/*
 * Test.java
 *
 * Created on October 18, 2007, 3:53 PM
 */
package com.lct.eq2.gui;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.DefaultBoundedRangeModel;
import com.lct.eq2.util.FileDownload;
import java.awt.Toolkit;

/**
 *
 * @author  waitek
 */
public class GetNewVersion extends javax.swing.JFrame {
    LiteGui parentGui;
    Properties props;
    String urlVersion;

    /** Creates new form Test */
    public GetNewVersion(LiteGui parent, boolean startup, String version) {
        this.setIconImage(Toolkit.getDefaultToolkit().getImage("eq2.png"));
        initComponents();
        this.parentGui = parent;
        props = parent.getProgramProperties();
        this.urlVersion = version;
        if (startup) {
            this.setAlwaysOnTop(true);
        }
    }

    public void checkVersion(boolean startup) {
        this.parentGui.checkVersion(startup);
    }

    public void setStatus(String status) {
        this.jLabel2.setText(status);
    }

    private void saveProperties() {
        String propFile = "lct.properties";
        try {
            FileOutputStream out = new FileOutputStream(propFile);
            this.props.store(out, "updated");
            out.close();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public GetNewVersion() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jProgressBar1 = new javax.swing.JProgressBar();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("LCT Updater");

        jButton1.setText("Download and Restart");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(26, 26, 26)
                .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
                .add(27, 27, 27)
                .add(jLabel2))
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jProgressBar1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                .add(16, 16, 16))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 13, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButton1))
                .add(18, 18, 18)
                .add(jProgressBar1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        saveProperties();
        DownloadHelper helper = new DownloadHelper(this);
        helper.start();
    }//GEN-LAST:event_jButton1ActionPerformed

    public class DownloadHelper extends Thread implements Runnable {

        GetNewVersion parent;

        public DownloadHelper(GetNewVersion parent) {
            this.parent = parent;
        }

        public void run() {
            String currentFile = this.parent.props.getProperty("currentFile");
            switch (Integer.parseInt(currentFile)) {
                case 0:
                    currentFile = "LCT1.jar";
                    break;
                case 1:
                    currentFile = "LCT.jar";
                    break;
                default:
                    break;
            }
            FileDownload download = new FileDownload(this.parent);
            jLabel2.setText("Downloading latest version...");
            download.download("http://lct-eq2.googlecode.com/files/LCT" + urlVersion + ".jar", currentFile);
            this.parent.parentGui.setRestart(true);
            this.parent.parentGui.dispose();
            this.parent.dispose();
        }
    };
    
        public void setProgressMax(int max) {
        DefaultBoundedRangeModel model = (DefaultBoundedRangeModel)this.jProgressBar1.getModel();
        //model.setMaximum(max);
        this.jProgressBar1.setStringPainted(true);
        this.jProgressBar1.setMinimum(0);
        this.jProgressBar1.setMaximum(max);
    //System.out.println("max = " + max);
    }

    public void setProgress(long progress) {
        DefaultBoundedRangeModel model = (DefaultBoundedRangeModel) this.jProgressBar1.getModel();
        int i = 0;
        i += progress;
        this.jProgressBar1.setValue(i);
    //System.out.println("Progress: " + i);
    //model.setValue(i);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GetNewVersion().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JProgressBar jProgressBar1;
    // End of variables declaration//GEN-END:variables
}
